package com.springboot.ding.monitor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @description:
 * @author: zyj
 * @create: 2020-07-23 18:30
 **/
public class MonitorThreaPool {
    //队列大小
    static final int core_pool_size   = 100;//核心线程池大小
    static final int maximum_pool_size   = 500;//最大线程池大小
    static final int keep_alive_time   = 1000;//核心线程池大小

    private static ExecutorService pool;


    public synchronized static ExecutorService  getInstance(){

        if(null == pool){
            pool = new ThreadPoolExecutor(core_pool_size, maximum_pool_size, keep_alive_time, TimeUnit.MILLISECONDS, MonitorQueue.getMonitorQueue(), Executors.defaultThreadFactory(),new ThreadPoolExecutor.AbortPolicy());
        }


        return pool;
    }

    //生产入队
    public void produce(Runnable t) throws InterruptedException {
        MonitorQueue.getMonitorQueue().put(t);
    }

    //消费出队
    public  Object consume() throws InterruptedException {
        return MonitorQueue.getMonitorQueue().take();
    }

    // 获取队列大小
    public int size() {
        return MonitorQueue.getMonitorQueue().size();
    }
}
